<?php

class LoginAdminController extends Controller {

    function __construct() {
        parent::__construct();
        $this->loggin_type = 'loginadmin';
    }

    public function index() {
        $user = Session::get_object('user');
        (!$user || !$user->is_admin()) ? $this->render() : $this->redirect_to('config', 'index');
    }

    function process() {
        $nombre = $_POST['admin-name'];
        $pass = md5($_POST['admin-pass']);
        $id_empresa = $_POST['empresa'];
        Session::set('db', $id_empresa);
        update_db();
        $admin = Admin::find(array('nombreUsuario' => $nombre, 'pass' => $pass));
        if ($admin):
            $this->loggin($admin);
            $this->redirect_to('config', 'modificar_configuraciones');
        else:
            $this->set_message('Error', 'El Nombre de Usuario o Contraseña es incorrecto.');
            $this->redirect_to('loginadmin', 'index');
        endif;
    }

    public function before_load() {
        // Nothing to do before load
    }
    
    function close(){
        Session::destroy();
        $this->redirect_to('loginadmin', 'index');
    }

}

?>
